<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>药材管理 - 学生信息管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            padding: 30px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #667eea;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
        }

        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #667eea;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background 0.3s;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        .btn:hover {
            background: #5568d3;
        }

        .btn-small {
            padding: 5px 12px;
            font-size: 13px;
            margin: 0 3px;
        }

        .btn-danger {
            background: #e74c3c;
        }

        .btn-danger:hover {
            background: #c0392b;
        }

        .btn-success {
            background: #27ae60;
        }

        .btn-success:hover {
            background: #229954;
        }

        .search-box {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .search-box input[type="text"] {
            flex: 1;
            max-width: 400px;
            padding: 10px 15px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }

        .search-box input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
        }

        .search-info {
            color: #666;
            font-size: 14px;
            margin-bottom: 15px;
        }

        .search-info strong {
            color: #667eea;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: bold;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .no-data {
            text-align: center;
            padding: 40px;
            color: #999;
            font-size: 16px;
        }

        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #667eea;
            text-decoration: none;
        }

        .back-link:hover {
            text-decoration: underline;
        }

        .medicine-no {
            color: #667eea;
            font-weight: bold;
        }

        .alias {
            color: #666;
            font-size: 13px;
        }
    </style>
    <script>
        function confirmDelete(id, name) {
            if (confirm('确定要删除药材 "' + name + '" 吗？')) {
                window.location.href = '${pageContext.request.contextPath}/medicine?action=delete&id=' + id;
            }
        }

        function clearSearch() {
            window.location.href = '${pageContext.request.contextPath}/medicine?action=list';
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📚 药材管理</h1>
            <div>
                <a href="${pageContext.request.contextPath}/medicine?action=add" class="btn btn-success">➕ 添加药材</a>
            </div>
        </div>

        <!-- 搜索框 -->
        <div class="search-box">
            <form action="${pageContext.request.contextPath}/medicine" method="get" style="display: flex; gap: 10px; flex: 1;">
                <input type="hidden" name="action" value="search">
                <input type="text" name="keyword" placeholder="搜索药材名称、编号或功能介绍..." value="${keyword}">
                <button type="submit" class="btn">🔍 搜索</button>
                <c:if test="${not empty keyword}">
                    <button type="button" class="btn" onclick="clearSearch()">清除搜索</button>
                </c:if>
            </form>
        </div>

        <!-- 搜索结果提示 -->
        <c:if test="${not empty keyword}">
            <div class="search-info">
                搜索关键词：<strong>"${keyword}"</strong>，找到 <strong>${medicines.size()}</strong> 条结果
            </div>
        </c:if>

        <!-- 药材列表表格 -->
        <c:choose>
            <c:when test="${not empty medicines}">
                <table>
                    <thead>
                        <tr>
                            <th style="width: 10%;">编号</th>
                            <th style="width: 15%;">中药名</th>
                            <th style="width: 25%;">别名</th>
                            <th style="width: 10%;">详情</th>
                            <th style="width: 15%;">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach var="medicine" items="${medicines}">
                            <tr>
                                <td><span class="medicine-no">${medicine.medicineNo}</span></td>
                                <td><strong>${medicine.medicineName}</strong></td>
                                <td><span class="alias">${medicine.alias}</span></td>
                                <td>
                                    <a href="${pageContext.request.contextPath}/medicine?action=detail&id=${medicine.id}" 
                                       class="btn btn-small">查看详情</a>
                                </td>
                                <td>
                                    <a href="${pageContext.request.contextPath}/medicine?action=edit&id=${medicine.id}" 
                                       class="btn btn-small btn-success">修改</a>
                                    <button onclick="confirmDelete(${medicine.id}, '${medicine.medicineName}')" 
                                            class="btn btn-small btn-danger">删除</button>
                                </td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
            </c:when>
            <c:otherwise>
                <div class="no-data">
                    <c:choose>
                        <c:when test="${not empty keyword}">
                            😔 没有找到匹配的药材信息
                        </c:when>
                        <c:otherwise>
                            📭 暂无药材信息，请添加药材
                        </c:otherwise>
                    </c:choose>
                </div>
            </c:otherwise>
        </c:choose>

        <a href="${pageContext.request.contextPath}/admin.jsp" class="back-link">← 返回管理员页面</a>
    </div>
</body>
</html>

